﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using AjaxControlToolkit;
using System.Collections.Specialized;
using System.Xml;
using System.Text.RegularExpressions;

namespace CascadingDropdownExtenderExample
{
    public partial class _Default : System.Web.UI.Page
    {

        private static XmlDocument _document;
        private static Regex _inputValidationRegex;
        private static object _lock = new object();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public static XmlDocument Document
        {
            get
            {
                lock (_lock)
                {
                    if (_document == null)
                    {
                        // Read XML data from disk
                        _document = new XmlDocument();
                        _document.Load(HttpContext.Current.Server.MapPath("~/App_Data/SampleData.xml"));
                    }
                }
                return _document;
            }
        }

        public static string[] Hierarchy
        {
            get { return new string[] { "make", "model" }; }
        }

        public static Regex InputValidationRegex
        {
            get
            {
                lock (_lock)
                {
                    if (null == _inputValidationRegex)
                    {
                        _inputValidationRegex = new Regex("^[0-9a-zA-Z \\(\\)]*$");
                    }
                }
                return _inputValidationRegex;
            }
        }

        protected void Save(object sender, EventArgs e)
        { 
            MakeModelColor.Text = DropDownList1.Text + " " + DropDownList2.Text + " " +  DropDownList3.Text;
        }

        [WebMethod]
        [ScriptMethod]
        public static CascadingDropDownNameValue[] GetDropDownContents(string knownCategoryValues, string category)
        {
            // Get a dictionary of known category/value pairs
            StringDictionary knownCategoryValuesDictionary = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);

            // Perform a simple query against the data document
            return AjaxControlToolkit.CascadingDropDown.QuerySimpleCascadingDropDownDocument(Document, Hierarchy, knownCategoryValuesDictionary, category, InputValidationRegex);
        }
    }
}